package com.heima.leetcode.practice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 90. 子集 II
 * @date 2025/5/18 13:07
 */
public class E90 {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i <= nums.length; i++) {
            dfs(nums, result, i, new LinkedList<>(), 0);
        }
        return result;
    }

    private void dfs(int[] nums, List<List<Integer>> result, int length, LinkedList<Integer> currCombine, int start) {
        if (length == 0) {
            result.add(new ArrayList<>(currCombine));
            return;
        }
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i - 1]) {
                continue;
            }
            currCombine.push(nums[i]);
            dfs(nums, result, length - 1, currCombine, i + 1);
            currCombine.pop();
        }
    }
}
